package com.stylefeng.guns.rest.Util;
import com.stylefeng.guns.core.cache.CacheKit;
import com.stylefeng.guns.core.util.ComputerGpsZone;
import com.stylefeng.guns.core.util.Point;
import com.stylefeng.guns.rest.Tools.GlobalDefine;
import com.stylefeng.guns.rest.modular.App.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
@Component

public class InitDefineCache {
    @Resource
    private LineDatePriceMapper lineDatePriceMapper;
    @Resource
    private LineMileagePriceMapper lineMileagePriceMapper;
    @Resource
    private LinePriceMapper linePriceMapper;
    @Resource
    private LineRangePriceMapper lineRangePriceMapper;
    @Resource
    private LineSiteMapper lineSiteMapper;
    @Resource
    private LineTimePriceMapper lineTimePriceMapper;
    @Resource
    private LineTimeSlotMapper lineTimeSlotMapper;
    @Resource
    private OrderLineMapper orderLineMapper;

    @Autowired
    private EhCacheCacheManager cacheManager;
    @PostConstruct
    public void init() throws Exception {
        CacheKit.remove(GlobalDefine.DEFINEDEHCACHE,GlobalDefine.LINECACHE);
        try {
            LineInf lineInf=new LineInf();
            List<LineInf> listLine = orderLineMapper.selectListOrderLineForCache(lineInf);
            for (LineInf line : listLine) {
                String lineId = line.getOlId();
                //加载日期加价
                LineDatePrice lineDatePrice = new LineDatePrice();
                lineDatePrice.setOlId(lineId);
                line.setListDatePrice(lineDatePriceMapper.selectListLineDatePrice(lineDatePrice));
                //加载里程加价
                LineMileagePrice lineMileagePrice = new LineMileagePrice();
                lineMileagePrice.setOlId(lineId);
                line.setListMileagePrice(lineMileagePriceMapper.selectListLineMileagePrice(lineMileagePrice));
                //加载基本价
                LinePrice linePrice = new LinePrice();
                linePrice.setOlId(lineId);
                line.setListPrice(linePriceMapper.selectListLinePrice(linePrice));
                //加载区域价
                LineRangePrice lineRangePrice = new LineRangePrice();
                lineRangePrice.setOlId(lineId);
                line.setListRangePrice(lineRangePriceMapper.selectListLineRangePrice(lineRangePrice));
                //加载站点
                LineSite lineSite = new LineSite();
                lineSite.setOlId(lineId);
                line.setListSite(lineSiteMapper.selectListLineSite(lineSite));
                //时段加价
                LineTimePrice lineTimePrice = new LineTimePrice();
                lineTimePrice.setOlId(lineId);
                line.setListTimePrice(lineTimePriceMapper.selectListLineTimePrice(lineTimePrice));
                //排班
                LineTimeSlot lineTimeSlot = new LineTimeSlot();
                lineTimeSlot.setOlId(lineId);
                lineTimeSlot.setIsAll("1");
                line.setListTimeSlot(lineTimeSlotMapper.selectListLineTimeSlotForCache(lineTimeSlot));

                //System.out.println(JSON.toJSON(line).toString());
            }
            CacheKit.put(GlobalDefine.DEFINEDEHCACHE,GlobalDefine.LINECACHE,listLine);
           System.out.println(CacheKit.getKeys(GlobalDefine.DEFINEDEHCACHE).get(0));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取线路里程价
     * @param line 线路信息
     * @param km   距离
     * @return
     */
    public String getKmPrice(LineInf line,float km){
        BigDecimal price=new BigDecimal(0);
        java.text.DecimalFormat   df   =new  java.text.DecimalFormat("0.00");
        List<LineMileagePrice> list=line.getListMileagePrice();
        for(int i=0;i<list.size();i++){
            LineMileagePrice en=list.get(i);
            if(km>en.getLmpMinMileage()){//按照里程阶梯算价
                price=new BigDecimal(km-en.getLmpMinMileage()).multiply(en.getLmpPrice());
                i++;
                for(;i<list.size();i++){
                    en=list.get(i);
                    price=price.add(new BigDecimal(en.getLmpMaxMileage()-en.getLmpMinMileage()).multiply(en.getLmpPrice()));
                }
                break;
            }
        }
        return df.format(price.setScale(2,BigDecimal.ROUND_HALF_UP));
    }

    /**
     * 获取线路日期价格
     * @param line
     * @param sdate
     * @return
     */
    public  String getDatePrice(LineInf line,String sdate){
        //获取日期加价
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        java.text.DecimalFormat   df   =new java.text.DecimalFormat("0.00");
        List<LineDatePrice> list=line.getListDatePrice();
        for(LineDatePrice en:list){
            if(sdf.format(en.getLdpEndDate()).compareTo(sdate)>=0 && sdf.format(en.getLdpStartDate()).compareTo(sdate)<=0  ){
                return df.format(en.getLdpPrice());
            }
        }
        return "0.00";
    }

    /**
     * 获取线路时段价格
     * @param line
     * @param stime
     * @return
     */
    public  String getTimePrice(LineInf line,String stime){
        //获取日期加价
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");
        java.text.DecimalFormat   df   =new   java.text.DecimalFormat("0.00");
        List<LineTimePrice> list=line.getListTimePrice();
        for(LineTimePrice en:list){
            String eTime=sdf.format(en.getLtpEtime());
            String sTtime=sdf.format(en.getLtpStime());
            if(eTime.compareTo(stime)>=0 && sTtime.compareTo(stime)<=0  ){
                return df.format(en.getLtpPrice());
            }
        }
        return "0.00";
    }
    /**
     * 根据线路、城市和坐标点获取当前线路的区域价格
     * @param lgn
     * @param lat
     * @param line
     * @param cityId
     * @return 区域价格
     */
    public  BigDecimal getCityZonePriceByPoint(double lgn,double lat,LineInf line,String cityId){
        BigDecimal price=new BigDecimal(0);
        List<LineRangePrice> listZonePoint=line.getListRangePrice();
        for(int i=0;i<listZonePoint.size();i++){
            LineRangePrice en=listZonePoint.get(i);
            if(!en.getLcId().equals(cityId)){
                continue;
            }
            String points=en.getLrpPositionSets();
            if(points==null || points.length()<5){
                continue;
            }
            String[] pointArry=points.split(";");
           /* ArrayList<Double> listLng=new ArrayList();
            ArrayList<Double> listLat=new ArrayList();*/
            ArrayList<Point> mPoints= new ArrayList();
            for(int j=0;j<pointArry.length;j++){
                String lngLat[]=pointArry[j].split(",");
                //coordinates1[j]=new Coordinate(Double.parseDouble(lngLat[0]),Double.parseDouble(lngLat[1]));
                /*listLng.add(Double.parseDouble(lngLat[0]));
                listLat.add(Double.parseDouble(lngLat[1]));*/
                mPoints.add(new Point(Double.parseDouble(lngLat[0]),Double.parseDouble(lngLat[1])));
            }
            Point point=new Point(lgn,lat);
            if(ComputerGpsZone.isPolygonContainsPoint(mPoints, point) || ComputerGpsZone.isPointInPolygonBoundary(mPoints, point)){
                return en.getLrpPrice();
            }
            /*if(ComputerGpsZone.isPointInPolygon(lgn,lat,listLng,listLat)){
                return en.getLrpPrice();
            }*/
        }
        return price;
    }

    /**
     * 根据城市和经纬度坐标计算本坐标是否在此城市内
     * @param lgn
     * @param lat
     * @param line
     * @param cityId
     * @return 返回城市的区域Id
     */
    public  String checkPoint(double lgn,double lat,LineInf line,String cityId){
        String flg="";
        List<LineRangePrice> listZonePoint=line.getListRangePrice();
        for(LineRangePrice en:listZonePoint){
            if(!en.getLcId().equals(cityId)){
                continue;
            }
            String points=en.getLrpPositionSets();
            if(points==null || points.length()<5){
                continue;
            }
            String[] pointArry=points.split(";");
            //Coordinate[] coordinates1 = new Coordinate[(pointArry.length+1)];
            /*ArrayList<Double> listLng=new ArrayList();
            ArrayList<Double> listLat=new ArrayList();*/
            ArrayList<Point> mPoints= new ArrayList();
            for(int j=0;j<pointArry.length;j++){
                String lngLat[]=pointArry[j].split(",");
                //coordinates1[j]=new Coordinate(Double.parseDouble(lngLat[0]),Double.parseDouble(lngLat[1]));
               /* listLng.add(Double.parseDouble(lngLat[0]));
                listLat.add(Double.parseDouble(lngLat[1]));*/
                mPoints.add(new Point(Double.parseDouble(lngLat[0]),Double.parseDouble(lngLat[1])));
            }
            Point point=new Point(lgn,lat);
            if(ComputerGpsZone.isPolygonContainsPoint(mPoints, point) || ComputerGpsZone.isPointInPolygonBoundary(mPoints, point)){
                return en.getLczId();
            }
            /*if(ComputerGpsZone.isPointInPolygon(lgn,lat,listLng,listLat)){
                return en.getLczId();
            }*/
        }
        return flg;
    }
}
